package INol;

import org.springframework.util.DigestUtils;

import java.io.*;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.20.16.04
 */
public class Until {
    static int num=0;
    static Map<String,Integer> map=new HashMap<>();
    public static void main(String[] args) {

    }
    //统计文件数量
   public static int count(String dir,String ext){
       File dirs=new File(dir);
        return count(dirs,ext);
   }
    //统计文件数量
    public static int count(File dir,String ext){
       for(File f:dir.listFiles()){
           if(f.isDirectory()){
               count(f,ext);
           }else if(f.isFile()&&f.getName().endsWith(ext)){
               ++num;
           }
       }
       return num;
    }
    //获取文件类型
public static Map<String,Integer> list(File dir){
        for(File f:dir.listFiles()){
            if(f.isDirectory()){
              list(f);
            }else if(f.isFile()){
                String fn=f.getName();
                int index=fn.lastIndexOf(".");
                String ext=index==-1?fn:fn.substring(index);
                if(map.containsKey(ext)){
                    map.put(ext,map.get(ext)+1);
                }else {
                    map.put(ext,1);
                }
            }
        }
      return map;
}
//删除文件
    public static boolean delFile(String file){
        return delFile(new File(file));
}
    public static boolean delFile(File file){
        boolean f=false;
        if(file.isFile()&file.exists()){
            file.delete();
            f=true;
        }
        return f;
    }
//删除目录
    public static void delDir(String dir){
        delDir(new File(dir));
    }
    public static void delDir(File dir){
        File[] fs=dir.listFiles();
        if(fs.length==0){
            dir.delete();
        }else {
            for(File f:fs){
                if(f.isDirectory()){
                    delDir(f);
                }else if(f.isFile()){
                    f.delete();
                }
            }
        }
    }

//统计单一文件行数
    public static long countSourcrRows(File file){
        long rows=0;
        try(BufferedReader br=new BufferedReader(new FileReader(file))) {
         rows=br.lines().count();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rows;
    }
//统计文件行数
    public static long countDirRows(File dir){
        long rows = 0;
        for(File f:dir.listFiles()){
            if(f.isDirectory()){
            countDirRows(f);
            }else if(f.isFile()&&f.getName().endsWith("")){
                 rows = countSourcrRows(f);
            }
        }
        return rows;
    }
    //md5码
    public static boolean isSames(String one,String two){
        return isSames(new File(one),new  File(two));
    }
    public static boolean isOk(File one,File two){
        boolean f=false;
        try(var fo=new FileInputStream(one);var ft=new FileInputStream(two)) {
        String f1= DigestUtils.md5DigestAsHex(fo);
        String f2=DigestUtils.md5DigestAsHex(ft);
        if(f1.equalsIgnoreCase(f2)){
            f=true;
        }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return f;
    }
//
    public static boolean isSames(File one,File two){
        boolean f=false;
        if(one.length()==two.length()){
            try(var fo=new FileInputStream(one);var ft=new FileInputStream(two)) {
                long i=0;
                while (fo.read()==ft.read()){
                    ++i;
                    if(i==one.length()*0.01){
                        f=true;
                        break;
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }
//
    public static String getFileType(String file){
        return getFileType(new File(file));
    }
    public static String getFileType(File file){
       String e="未知";
       var map=new HashMap<String,String>();
       map.put("jpg", "FFD8FF"); //JPEG (jpg)
        map.put("png", "89504E47");  //PNG (png)
        map.put("gif", "47494638");  //GIF (gif)
        map.put("tif", "49492A00");  //TIFF (tif)
        map.put("bmp", "424D"); //Windows Bitmap (bmp)
        map.put("dwg", "41433130"); //CAD (dwg)
        map.put("html", "68746D6C3E");  //HTML (html)
        map.put("rtf", "7B5C727466");  //Rich Text Format (rtf)
        map.put("xml", "3C3F786D6C");
        map.put("zip", "504B0304");
        map.put("rar", "52617221");
        map.put("psd", "38425053");  //Photoshop (psd)
        map.put("eml", "44656C69766572792D646174653A");  //Email [thorough only] (eml)
        map.put("dbx", "CFAD12FEC5FD746F");  //Outlook Express (dbx)
        map.put("pst", "2142444E");  //Outlook (pst)
        map.put("xls", "D0CF11E0");  //MS Word
        map.put("doc", "D0CF11E0");  //MS Excel 注意：word 和 excel的文件头一样
        map.put("mdb", "5374616E64617264204A");  //MS Access (mdb)
        map.put("wpd", "FF575043"); //WordPerfect (wpd)
        map.put("eps", "252150532D41646F6265");
        map.put("ps", "252150532D41646F6265");
        map.put("pdf", "255044462D312E");  //Adobe Acrobat (pdf)
        map.put("qdf", "AC9EBD8F");  //Quicken (qdf)
        map.put("pwl", "E3828596");  //Windows Password (pwl)
        map.put("wav", "57415645");  //Wave (wav)
        map.put("avi", "41564920");
        map.put("ram", "2E7261FD");  //Real Audio (ram)
        map.put("rm", "2E524D46");  //Real Media (rm)
        map.put("mpg", "000001BA");  //
        map.put("mov", "6D6F6F76");  //Quicktime (mov)
        map.put("asf", "3026B2758E66CF11"); //Windows Media (asf)
        map.put("mid", "4D546864");  //MIDI (mid)

        String ext=getFileHeaderInfo(file,14);
        for(String k:map.keySet()){
            if(ext.startsWith(map.get(k))){
                e=k;
                break;
            }
        }
        return e;
    }
//
    public static String getFileHeaderInfo(File file,int size){
        StringBuilder sub=new StringBuilder();
        byte[] buf=new byte[size];
        try(var fis=new FileInputStream(file)) {
            fis.read(buf);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for(byte b:buf){
            sub.append(String.format("%02d",b));
        }
        return sub.toString();
    }
    //
    public static void copyFile(File src,File dst){
        try(var fis=new FileInputStream(src);var fos=new FileOutputStream(dst)) {
         int len=-1;
         byte[] buffer=new byte[1024];
         while ((len=fis.read(buffer))>0){
             fos.write(buffer,0,len);
         }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static  void copyFile(String src,String dst){
        copyFile(new File(src),new File(dst));
    }
    public static void copyDir(File src,File dst){
        if(!src.isDirectory()){
            return;
        }
        if(!dst.exists()){
            dst.mkdirs();
        }
        File[] files=src.listFiles();
        for(File f:files){
            if(f.isDirectory()){
                copyFile(f,new File(dst.getPath(),f.getName()));
            }else if(f.isFile()){
                copyFile(f,new File(dst.getPath(),f.getName()));
            }
        }
    }
    //
    public static void copyDir(String src,String dst){
        copyDir(new File(src),new File(dst));
    }
    public static void  rename(String file){
        rename(new File(file));
    }
    public static void  rename(File file){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssS");
        Random rand=new Random();
        String fn=String.format("%s_%05d",sdf.format(new Date()),rand.nextInt(99999)+getExt(String.valueOf(file)));
        file.renameTo(new File(file.getPath(),fn));
    }
    public static String getExt(String file){
        String fn=file.toLowerCase(Locale.ROOT);
        int i=fn.lastIndexOf(".");
        return i==-1?fn:fn.substring(i);
    }
    //
    public static Map<String,Integer>countJdk(String file){
        return countJdk(new File(file));
    }
    public static Map<String,Integer>countJdk(File file){
       for(File f:file.listFiles()){
           if(f.isDirectory()){
               countJdk(f);
           }else if(f.isFile()&&f.getName().endsWith(".java")){
               StringBuilder sbu=new StringBuilder();
               int rows=0;
               try(var br=new BufferedReader(new FileReader(f))) {
                   while (br.ready()){
                       sbu.append(br.readLine());
                   }

               } catch (FileNotFoundException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               }
               Pattern pattern=Pattern.compile("[a-zA-Z]{2,}");
               Matcher m=pattern.matcher(sbu.toString());
               while (m.find()){
                   String w=m.group();
                   if(map.containsKey(w)){
                       map.put(w,map.get(w)+1);
                   }else {
                       map.put(w,1);
                   }
               }
           }

       }
       return map;
    }

}

